<template>
  <div class="tutoring-audit">
    <h2>家教信息审核管理</h2>
    
    <div class="filter-area">
      <el-form :inline="true">
        <el-form-item label="状态">
          <el-select v-model="statusFilter" placeholder="全部状态" clearable @change="handleFilter" style="width: 150px;">
            <el-option label="已下架" :value="0" />
            <el-option label="可用" :value="1" />
            <el-option label="已完成" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="教学科目">
          <el-input v-model="subjectFilter" placeholder="请输入科目关键字" clearable @keyup.enter="handleFilter" style="min-width: 200px;" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleFilter">搜索</el-button>
          <el-button @click="resetFilter">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    
    <el-table :data="filteredTutorings" style="width: 100%" v-loading="loading">
      <el-table-column type="expand">
        <template #default="props">
          <el-descriptions :column="2" border>
            <el-descriptions-item label="详细描述">
              <div class="description-content">{{ props.row.description }}</div>
            </el-descriptions-item>
            <el-descriptions-item label="可用时间">{{ props.row.availableTime }}</el-descriptions-item>
            <el-descriptions-item label="教师真实姓名">{{ props.row.teacherRealName || '未提供' }}</el-descriptions-item>
            <el-descriptions-item label="学历">{{ props.row.education || '未提供' }}</el-descriptions-item>
            <el-descriptions-item label="专业">{{ props.row.major || '未提供' }}</el-descriptions-item>
            <el-descriptions-item label="学校">{{ props.row.school || '未提供' }}</el-descriptions-item>
            <el-descriptions-item label="所在省份">{{ props.row.province || '未提供' }}</el-descriptions-item>
            <el-descriptions-item label="创建时间">{{ formatDateTime(props.row.createTime) }}</el-descriptions-item>
            <el-descriptions-item label="最后更新">{{ formatDateTime(props.row.updateTime) }}</el-descriptions-item>
          </el-descriptions>
        </template>
      </el-table-column>
      <el-table-column prop="id" label="ID" width="60" />
      <el-table-column label="教师信息" width="200">
        <template #default="scope">
          <div class="teacher-info">
            <el-avatar :size="40" :src="scope.row.teacherAvatar" />
            <div class="teacher-detail">
              <div>{{ scope.row.teacherName }}</div>
              <div class="teacher-real-name" v-if="scope.row.teacherRealName">
                {{ scope.row.teacherRealName }}
              </div>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="title" label="标题" width="200" />
      <el-table-column prop="subjects" label="教学科目" width="150" />
      <el-table-column prop="targetLevel" label="目标学生" width="120" />
      <el-table-column prop="hourlyRate" label="时薪" width="100">
        <template #default="scope">
          ¥{{ scope.row.hourlyRate }}/小时
        </template>
      </el-table-column>
      <el-table-column prop="teachingMethod" label="教学方式" width="100" />
      <el-table-column prop="location" label="地点" width="150" />
      <el-table-column prop="status" label="状态" width="130">
        <template #default="scope">
          <el-tag :type="getStatusType(scope.row.status)">
            {{ getStatusText(scope.row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="250">
        <template #default="scope">
          <el-button 
            v-if="scope.row.status !== 1" 
            type="success" 
            size="small" 
            @click="updateStatus(scope.row, 1)">
            设为可用
          </el-button>
          <el-button 
            v-if="scope.row.status !== 0" 
            type="warning" 
            size="small" 
            @click="updateStatus(scope.row, 0)">
            下架
          </el-button>
          <el-button 
            v-if="scope.row.status !== 2" 
            type="info" 
            size="small" 
            @click="updateStatus(scope.row, 2)">
            标为已完成
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { tutoringService } from '@/api/tutoring'
import { formatDateTime } from '@/utils/format'

const tutorings = ref([])
const loading = ref(false)
const statusFilter = ref(null)
const subjectFilter = ref('')

// 获取所有家教信息
const fetchAllTutorings = async () => {
  loading.value = true
  try {
    const res = await tutoringService.adminGetAllTutoring()
    if (res.code === 0) {
      tutorings.value = res.data
    } else {
      ElMessage.error(res.msg || '获取家教信息失败')
    }
  } catch (error) {
    console.error('获取家教信息失败:', error)
    ElMessage.error('获取家教信息失败')
  } finally {
    loading.value = false
  }
}

// 根据状态和科目筛选家教信息
const filteredTutorings = computed(() => {
  let result = [...tutorings.value]
  
  // 按状态筛选
  if (statusFilter.value !== null) {
    result = result.filter(item => item.status === statusFilter.value)
  }
  
  // 按科目筛选
  if (subjectFilter.value) {
    const keyword = subjectFilter.value.toLowerCase()
    result = result.filter(item => 
      item.subjects && item.subjects.toLowerCase().includes(keyword)
    )
  }
  
  return result
})

// 处理筛选
const handleFilter = () => {
  // 筛选逻辑已由计算属性处理
}

// 重置筛选
const resetFilter = () => {
  statusFilter.value = null
  subjectFilter.value = ''
}

// 获取状态标签类型
const getStatusType = (status) => {
  const types = {
    0: 'danger',   // 已下架
    1: 'success',  // 可用
    2: 'info'      // 已完成
  }
  return types[status] || 'info'
}

// 获取状态文本
const getStatusText = (status) => {
  const texts = {
    0: '已下架',
    1: '可用',
    2: '已完成'
  }
  return texts[status] || '未知状态'
}

// 更新家教信息状态
const updateStatus = async (tutoring, status) => {
  try {
    const statusText = getStatusText(status)
    await ElMessageBox.confirm(
      `确定要将家教信息「${tutoring.title}」的状态修改为「${statusText}」吗？`,
      '修改状态',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )
    
    const res = await tutoringService.adminAuditTutoring(tutoring.id, status)
    if (res.code === 0) {
      ElMessage.success(`状态修改成功`)
      tutoring.status = status
    } else {
      ElMessage.error(res.msg || '状态修改失败')
    }
  } catch (error) {
    if (error !== 'cancel') {
      console.error('状态修改失败:', error)
      ElMessage.error('状态修改失败')
    }
  }
}

// 组件挂载时获取数据
onMounted(() => {
  fetchAllTutorings()
})
</script>

<style scoped>
.tutoring-audit {
  padding: 20px;
}

.filter-area {
  margin-bottom: 25px;
  background-color: #f5f7fa;
  padding: 15px;
  border-radius: 4px;
}

.teacher-info {
  display: flex;
  align-items: center;
  gap: 10px;
}

.teacher-detail {
  display: flex;
  flex-direction: column;
}

.teacher-real-name {
  font-size: 12px;
  color: #606266;
}

.description-content {
  max-height: 150px;
  overflow-y: auto;
  white-space: pre-line;
}
</style> 